layui.use(["layer","jquery","element"],function(){
    var layer = layui.layer
        ,$ = layui.$
        ,element = layui.element;

    //校验登录状态
    if(isNotLogIn()){
        return;
    }
    var managerId = getCookie("managerId");
    //获取消息数目
    $.ajax({
        url : "/CivilAffairs/msg/getMsgNum",
        type : "post",
        async : false,
        data: {"managerId":managerId},
        dataType : "json",
        success : function(result) {
            if("9999"==result.status){
                layer.msg(result.detail,{icon:2});
                setTimeout(function () {
                    logOut(true);
                },500);
            }else{
                $("#msgNum").text(result.data);
            }
        },
        error : function(result) {
            reqError(result,"获取未读消息异常");
        }
    });


    var managerLevel = getCookie("managerLevel");
    var superOption = "<dd><a href=\"javascript:;\"  data-url=\"./manager/page/user/userInfo.html\" data-icon=\"&#xe62a;\">\n" +
        "<i class=\"layui-icon iconfont icon-shezhi1\" data-icon=\"icon-shezhi1\">&#xe62a;</i>\n" +
        "                        <cite>基本资料</cite></a></dd>\n" +
        "                    <dd><a href=\"javascript:;\" data-url=\"./manager/page/user/changePwd.html\" data-icon=\"&#xe614;\">\n" +
        "                        <i class=\"layui-icon iconfont icon-shezhi1\" data-icon=\"icon-shezhi1\">&#xe614;</i>\n" +
        "                        <cite>修改密码</cite>\n" +
        "                        </a>\n" +
        "                    </dd>"
    if("99"==managerLevel || "02"==managerLevel){
        superOption += "<dd><a href=\"javascript:;\" data-url=\"./manager/page/user/allUsers.html\" data-icon='&#xe613;'>" +
            "<i class=\"layui-icon iconfont icon-shezhi1\" data-icon=\"icon-shezhi1\">&#xe613;</i><cite>用户管理</cite></a></dd>";
    }
    $(".userOption").append(superOption);

    var tabIdIndex = 0;
    //点击左侧标签栏,添加内容
    $("body").on("click",".layui-nav .layui-nav-item a",function(){
        //校验登录状态
        if(isNotLogIn()){
            return;
        }
        var _this= $(this);
        var title = _this.find("cite").text();
        //如果是新窗口
        if(-1==hasTab(title)){
            var dataUrl = _this.attr("data-url");
            var iconCode = _this.attr("data-icon");
            if(!isNull(dataUrl)){
                var newTitle = "<i type='cite' class=\"iconfont icon-caozuo layui-icon\">"+iconCode+"</i><cite>"+title+"</cite><i class=\"layui-icon layui-unselect layui-tab-close\" data-id='"+tabIdIndex+"'>&#x1006;</i>";
                element.tabAdd("bodyTab", {
                    title : newTitle,
                    content :"<iframe src='"+dataUrl+"' data-id='"+tabIdIndex+"'></frame>",
                    id : new Date().getTime()
                });
                element.tabChange("bodyTab", getLayIdByTitle(title));
            }
        }else{
            element.tabChange("bodyTab",getLayIdByTitle(title));
        }
        moveLeft(title);
    });

    //点击上边的导航栏,左侧导航栏也改动
    $("body").on("click",".top_tab li",function(){
        var checkLi = $(this);
        var showTitle = checkLi.find("cite").text();
        moveLeft(showTitle);
    });
    //移动左侧侧边栏
    function moveLeft(showTitle){
        $(".navBar li.layui-this").removeClass("layui-this");
        $(".navBar li").each(function () {
            if(showTitle == $(this).find("cite").text()){
                $(this).addClass("layui-this");
            }
        });
    }

    //点击关闭页面
    $("body").on("click",".top_tab li i.layui-tab-close",function(){
        var index = $(this).parents("li").index();
        element.tabDelete("bodyTab",$(this).parents("li").attr("lay-id"));
        //同时修改侧边栏的显示
        var isShowTitle = $(".layui-tab-title.top_tab li.layui-this").find("cite").text();

        var leftShowTitle = $(".navBar li.layui-this").find("cite").text();
        //如果在选中的不是要显示的,则获取要显示的layui-id,显示该元素
        if(isShowTitle != leftShowTitle){
            //左侧侧边栏点击选中
            moveLeft(isShowTitle);
        }
        return false;
    });

    /**
     * 根据title获取layId
     * @param title
     * @returns {*}
     */
    function getLayIdByTitle(title){
        var layId = '';
        $("#top_tabs li").each(function(){
            if($(this).find("cite").text() == title){
                layId = $(this).attr("lay-id");
            }
        });
        return layId;
    }
    //通过title判断tab是否存在
    function hasTab(title){
        var tabIndex = -1;
        $("#top_tabs li").each(function(){
            if($(this).find("cite").text() == title){
                tabIndex = 1;
            }
        });
        return tabIndex;
    }
    //锁屏
    function lockPage(){
        var managerId = getCookie("managerId");
        if(isNull(managerId)){
            layer.msg("请先登录账号");
            logOut(true);
            return;
        }
        layer.open({
            title : false,
            type : 1,
            content : '	<div class="admin-header-lock" id="lock-box">'+
            '<div class="admin-header-lock-img"><img id="userFace" src=""/></div>'+
            '<div class="admin-header-lock-name" id="lockUserName"></div>'+
            '<div class="input_btn">'+
            '<input type="password" class="admin-header-lock-input layui-input" autocomplete="off" placeholder="请输入登录密码解锁.." name="lockPwd" id="lockPwd" />'+
            '<button class="layui-btn" id="unlock">解锁</button>'+
            '</div></div>',
            closeBtn : 0,
            shade : 0.9
        });
        setTimeout(function(){
            $("#userFace").attr("src",getCookie("managerFacePic"));
            $("#lockUserName").text(getCookie("managerName"));
            $(".admin-header-lock-input").focus();
        },50);
    }
    $(".lockcms").on("click",function(){
        //设置为锁屏状态
        window.sessionStorage.setItem("lockcms",true);
        lockPage();
    });
    //判断是否显示锁屏
    if(window.sessionStorage.getItem("lockcms") == "true"){
        lockPage();
    }
    // 解锁
    $("body").on("click","#unlock",function(){
        if($(this).siblings(".admin-header-lock-input").val() == ''){
            layer.msg("请输入解锁密码！");
            $(this).siblings(".admin-header-lock-input").focus();
        }else{
            var username = getCookie("username");
            if(isNull(username)){
                layer.msg("请先登录账号");
                logOut(true);
                return;
            }
            var unlockPwd = $(this).siblings(".admin-header-lock-input").val();
            $.ajax({
                url : "/CivilAffairs/manager/unlock",
                type : "post",
                async : false,
                data: {"username":username,"password":unlockPwd},
                dataType : "json",
                success : function(result) {
                    if("0000"==result.status){
                        //设置锁屏状态为未锁屏
                        window.sessionStorage.setItem("lockcms",false);
                        $(this).siblings(".admin-header-lock-input").val('');
                        layer.closeAll("page");
                    }else{
                        layer.msg("密码错误，请重新输入！");
                        $(this).siblings(".admin-header-lock-input").val('').focus();
                    }
                },
                error : function(result) {
                    layer.close(index);
                    console.log("解锁屏幕异常:"+result.status+","+result.statusMessage);
                    layer.msg("解锁屏幕异常", { icon: 2 });
                    $(this).siblings(".admin-header-lock-input").val('').focus();
                }
            });
        }
    });

    //判断回车
    $(document).keyup(function (e) {//捕获文档对象的按键弹起事件
        if (e.keyCode == 13) {
            $("#unlock").click();
        }
    });
});
//登录验证
var type = "1";
$(function(){
    'use strict';
    layui.use(["jquery","element","layer"],function () {
        var $ = layui.$
            ,element = layui.element
            ,layer = layui.layer;
        var managerName = getCookie("managerName");
        var picSrc= getCookie("managerFacePic");
        $(".userName").html(managerName);
        $(".userFace").attr("src",picSrc);
    });
});

/**
 * 退出登录
 */
function isLogOut(){
    layui.use(["layer"],function () {
        //示范一个公告层
        layer.open({
            type: 1
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: '300px;'
            ,shade: 0.8
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,btn: ['确认','取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: '<div style="padding: 20px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;text-align:center;"> 确认退出登录?</div>'
            ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.find('.layui-layer-btn0').attr({
                    onclick: 'logOut(true)'
                    ,target: '_blank'
                });
            }
        });
    });
}